package org.project.task.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.project.task.common.R;
import org.project.task.common.Search;
import org.project.task.entity.Admin;
import org.project.task.entity.Project;
import org.project.task.entity.Task;
import org.project.task.entity.User;
import org.project.task.service.AdminService;
import org.project.task.service.ProjectService;
import org.project.task.service.TaskService;
import org.project.task.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;
    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private ProjectService projectService;

    @PostMapping("/getList")
    public R<Page<Task>> getList(@RequestBody Search search) {
        try {
            Page<Task> pageData = taskService.page(
                    getPage(),
                    new QueryWrapper<Task>()
                            .like(StrUtil.isNotBlank(
                                            search.getContent()),
                                    "description",
                                    search.getContent()
                            )
                            .or()
                            .like(StrUtil.isNotBlank(
                                            search.getContent()),
                                    "title",
                                    search.getContent()
                            )
                            .orderByDesc("update_time")
            );

            for (Task task : pageData.getRecords()) {
                Admin admin = adminService.getById(task.getCreatorId());
                task.setCreator(admin);
                User user = userService.getById(task.getAssigneeId());
                task.setAssignee(user);
                Project project = projectService.getById(task.getProjectId());
                task.setProject(project);
            }
            return new R<Page<Task>>().success(pageData);
        } catch (Exception e) {
            return new R<Page<Task>>().error(e.getMessage());
        }
    }

    @GetMapping("/taskList")
    public R<List<Task>> taskList() {
        R<List<Task>> result = new R<>();
        List<Task> taskList = taskService.list();
        if (taskList != null && !taskList.isEmpty()) {
            result.success(taskList);
        } else {
            result.error("查询对象不存在!");
        }
        return result;
    }


    @GetMapping("/getById/{id}")
    public R<Task> getById(@PathVariable("id") Integer id) {
        R<Task> result = new R<>();
        Task task = taskService.getById(id);
        if (task != null) {
            Admin admin = adminService.getById(task.getCreatorId());
            task.setCreator(admin);
            result.success(task);
        } else {
            result.error("查询对象不存在!");
        }
        return result;
    }

    @PostMapping("/delete")
    public R<Task> delete(@RequestBody List<Long> ids) {
        R<Task> result = new R<>();
        if(taskService.removeByIds(ids)){
            result.success("删除成功！");
        } else {
            result.error("删除失败！");
        }
        return result;
    }

    @PostMapping("/insert")
    public R<Task> insert(@RequestBody Task task) {
        R<Task> result = new R<>();
        if (task != null) {
            task.setCreateTime(new Date());
            task.setUpdateTime(new Date());
            boolean rsg = taskService.save(task);
            if (rsg) {
                result.setData(task);
                result.success("添加成功！");
            } else {
                result.error("添加失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }

    @PostMapping("/update")
    public R<Task> update(@RequestBody Task task) {
        R<Task> result = new R<>();
        if (task != null) {
            task.setUpdateTime(new Date());
            boolean rsg = taskService.updateById(task);
            if (rsg) {
                result.setData(task);
                result.success("修改成功！");
            } else {
                result.error("修改失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }
}
